
#include <sceneview/sceneview.h>
#include <engine.h>
#include <session.h>
#include <GL/gl.h>
#include <qdebug.h>
#include <gfx/gl20/renderloop.h>
#include <scene/scene.h>

static QGLWidget *gl_share = 0;

cceSceneView::cceSceneView(QWidget *parent)
  : QGLWidget(parent, 0)
  , _renderLoop (0)
  , _scene(0)
	, _engine(0)
{
  qDebug () << this << "initialize with share " << gl_share;
  if (!gl_share)
    {
      gl_share = this;
    }
  CE_OBJECT_CONSTR;
}



void cceSceneView::initializeGL()
{
  _renderLoop = new ceRenderLoop();


  ceEngine* engine = GetEngine();
  _renderLoop->Initialize(engine->GetDevice());
}

void cceSceneView::paintGL()
{
  ceEngine* engine = GetEngine ();
  engine->SetWindow(this);
  _renderLoop->SetViewport(engine->GetDevice(), this);

  _renderLoop->Render(engine->GetDevice(), _scene, _camera, _projector);
}



void cceSceneView::CreateScene()
{
  ceScene* scene = new ceScene();
  SetScene(scene);
}

void cceSceneView::SetScene(ceScene *scene)
{
  CE_SET(_scene, scene);
}

ceScene* cceSceneView::GetScene()
{
  return _scene;
}


ceEngine* cceSceneView::GetEngine()
{
  if (!_engine)
    {
      _engine = cceSession::Get()->GetEngine();
    }
  return _engine;
}
